﻿/*
   Copyright 2011 Softisis

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Reflection;
using FOM.Impl.Exceptions;
using FOM.Interfaces;
using FOM.Utils;

namespace FOM.Impl.Converters
{
    /// <summary>
    /// This converter copies all properties of the object which is about to be replaced,
    /// to the target object (whathever the types ares).
    /// The matching rule is on the equality of property name.
    /// </summary>
    public class ReconnectOriginalChildrenConv : PropertyCopierConv
    {
        public ReconnectOriginalChildrenConv():base()
        {
            
        }

        /// <summary>
        /// Build the converter, with the possible choice to copy only value type AND string properties.
        /// </summary>
        /// <param name="valueTypesOnly">If true, Reconnect original source value types only.
        /// If false, all original children are copied</param>
        public ReconnectOriginalChildrenConv(bool valueTypesOnly)
        {
            ValueTypesOnly = valueTypesOnly;
        }

        public override object Convert(MappingContext ctx)
        {
            if (!ctx.IsNewInstance)
            {
                throw new FOMInvalidOperationException("You can't use ReconnectOriginalChildrenConv on an exising Instance. You must use NewInstance().");
            }
            return ConvertProtected(ctx, true);
        }
    }
}
